﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BusinessLogic;
using SDL;

namespace NotificationService
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {

        private static Dictionary<string, List<AbstractEvent>> eventList = new Dictionary<string, List<AbstractEvent>>();

        private Dictionary<string, string> observerList = new Dictionary<string, string>();

        public Service1()
        {
            //Підключення до БД
            var oConn = GlobalSettings.SDLСonnectionString.ToConnection();
            new ConnectionManager(oConn);
        }

        public void FireEvent(string userName, AbstractEvent evnt)
        {
            var ev = EventExtensions.getByOwnerAndName(userName, evnt.GetType().Name);
            var recives = ev.getRecivers();
            foreach (var user in recives)
            {
                if (!eventList.ContainsKey(user.Login))
                {
                    var evs = new List<AbstractEvent>();
                    eventList.Add(user.Login, evs);
                }
                eventList[user.Login].Add(evnt);
            }
        }

        public AbstractEvent GetEvent(string userId, string eventId)
        {
            return (eventList[userId].Find(i => i.ID == eventId));
        }

        public List<string> GetEvents(string userId)
        {
            if (eventList.ContainsKey(userId))
            {
                var res = new List<string>();
                foreach (var e in eventList[userId])
                {
                    res.Add(e.ID);
                }
                return res;
            }
            return null;
        }

        public void OnEventRecived(string userId, string eventId)
        {
            eventList[userId].RemoveAll(i => i.ID == eventId);
        }
    }


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute("ttt", Namespace = "", IsNullable = false)]
    public class TestEvent : AbstractEvent
    {

        private string testField1Field;

        private decimal testField2Field;

        /// <remarks/>
        public string testField1
        {
            get
            {
                return this.testField1Field;
            }
            set
            {
                this.testField1Field = value;
            }
        }

        /// <remarks/>
        public decimal testField2
        {
            get
            {
                return this.testField2Field;
            }
            set
            {
                this.testField2Field = value;
            }
        }
    }
}
